import { defineStore } from "pinia";
import {AuthState} from "@/interface";
import {AuthOperation, Menu, SysUserInfo, TokenInfo} from "@/interface/api";
import piniaPersistConfig from "@/config/piniaPersist";
import {getAllBreadcrumbList, getShowMenuList, renderMenuOptions} from "@/helper";

export const authStore = defineStore({
    id: "qiyi183-blog-dept",
    state: (): AuthState => ({
        user: null,
        token: null,
        menuList: [],
        showMenuList: [],
        flatMenuList: [],
        breadcrumbList: {},
        operations: {},
    }),
    getters: {},
    actions: {
        setUser(userInfo: SysUserInfo | null) {
            this.user = userInfo;
        },
        setToken(tokenInfo: TokenInfo | null) {
            this.token = tokenInfo;
        },
        setMenuList(menus: Menu[]) {
            this.menuList = menus;
            this.showMenuList = getShowMenuList(this.menuList);
            renderMenuOptions(menus, this.flatMenuList);
            this.breadcrumbList = getAllBreadcrumbList(this.flatMenuList);
        },
        setOperations(operations: AuthOperation) {
            this.operations = operations;
        },
        resetAuth() {
            this.user = null;
            this.token = null;
            this.menuList = [];
            this.showMenuList = [];
            this.flatMenuList = [];
            this.breadcrumbList = {};
            this.operations = {};
        }
    },
    persist: piniaPersistConfig("qiyi183-blog-dept")
});
